package com.weixing.mall.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.api.model.vo.GoodsDetailsVo;
import com.weixing.mall.provider.api.model.vo.ProductStockDetailsVo;
import com.weixing.mall.provider.api.model.vo.ProductStockVo;
import com.weixing.mall.provider.api.service.GoodsFeign;
import com.weixing.mall.provider.api.service.ProductStockServiceRpc;
import com.weixing.mall.provider.mapper.ProductStockTmpMapper;
import com.weixing.mall.provider.model.domain.Activity;
import com.weixing.mall.provider.model.domain.ActivityGoods;
import com.weixing.mall.provider.model.domain.ProductStockTmp;
import com.weixing.mall.provider.model.query.act.ProductStockTmpQuery;
import com.weixing.mall.provider.model.vo.act.ProductStockTmpVo;
import com.weixing.mall.provider.service.IActivityGoodsService;
import com.weixing.mall.provider.service.IActivityService;
import com.weixing.mall.provider.service.IProductStockTmpService;
import com.weixing.mall.util.ListUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库模块-商品库存表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-15
 */
@Service
public class ProductStockTmpServiceImpl extends ServiceImpl<ProductStockTmpMapper, ProductStockTmp> implements IProductStockTmpService {
    @Autowired
    private ProductStockServiceRpc productStockServiceRpc;
    @Autowired
    private IActivityGoodsService activityGoodsService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private GoodsFeign goodsFeign;

    @Override
    public IPage<ProductStockTmpVo> selectByDepotId(Page page, Long depotId) {
        IPage<ProductStockTmpVo> tt = baseMapper.selectByDepotId(page, depotId);
        if (tt == null || tt.getRecords().size() == 0) {//没有数据，则从仓库中进行拷贝，拷贝完成后在进行查询
            saveBit(depotId);
            return baseMapper.selectByDepotId(page, depotId);
        }
        return tt;
    }

    @Override
    public void resetStock(Long depotId) {
        baseMapper.delwhole();
        saveBit(depotId);

    }

    @Override
    public IPage<ProductStockTmpVo> selectOfPage(Page page, ProductStockTmpQuery productStockTmpQuery) {
        return baseMapper.selectOfPage(page, productStockTmpQuery);
    }

    @Override
    public void selectByQuery(ProductStockTmpQuery productStockTmpQuery) {
        List<ProductStockTmpVo> list = baseMapper.selectOfPage(productStockTmpQuery);
        Activity activity = activityService.getById(productStockTmpQuery.getActId());
        List<ActivityGoods> activityGoodsList = Lists.newArrayList();
        List<ActivityGoods> updateGoodsList = Lists.newArrayList();
        for (ProductStockTmpVo vo : list) {
            ActivityGoods goods = activityGoodsService.findByActIdAndItemId(activity.getId(), vo.getProductId());
            if (null != goods) {
                goods.setProductCount(goods.getProductCount() + vo.getEnableStock());
                updateGoodsList.add(goods);
            } else {
                GoodsDetailsVo itemVo = goodsFeign.selectGoodsDetails(vo.getGoodsId()).getData();
                ActivityGoods activityGoods = new ActivityGoods();
                BigDecimal markPrice = vo.getMarketPrice();
                BigDecimal discountPrice = vo.getDiscountPrice();
                BigDecimal discout = activity.getDiscount();
                // BigDecimal actPrice = markPrice.multiply(zk).setScale(2, BigDecimal.ROUND_HALF_UP);
                //新定义actprice价格计算  取discountPrice * discout   商品折扣价（零售价） 乘以 活动折扣
                BigDecimal actPrice = discountPrice.multiply(discout).setScale(2, BigDecimal.ROUND_HALF_UP);
                activityGoods.setActPrice(actPrice);
                if (vo.getEnableStock().equals(0)) {
                    continue;
                }
                activityGoods.setProductCount(vo.getEnableStock());
                activityGoods.setStyleNo(vo.getStyleNo());
                activityGoods.setProductSku(vo.getProductSkuDesp());
                activityGoods.setUpDown(WhetherEnum.YES.getValue());
                activityGoods.setProductPrice(markPrice);
                activityGoods.setProductName(vo.getGoodsName());
                activityGoods.setProductImg(CollUtil.isNotEmpty(itemVo.getGoodsImg()) ? itemVo.getGoodsImg().get(0) : null);
                activityGoods.setCatId(itemVo.getCatId());
                activityGoods.setCatName(itemVo.getCatName());
                activityGoods.setSupplierName(itemVo.getSupplierName());
                activityGoods.setProductId(vo.getProductId());
                activityGoods.setGoodsId(vo.getGoodsId());
                activityGoods.setGoodsSn(vo.getGoodsSn());
                activityGoods.setActId(activity.getId());
                activityGoods.setActType(activity.getActType().toString());
                activityGoods.setActType(activity.getActType().toString());
                activityGoods.setProductSkuDesp(vo.getProductSkuDesp());
                activityGoods.setProductFrom(vo.getDepotId().intValue());
                activityGoodsList.add(activityGoods);
            }
        }
        activityGoodsService.saveBatch(activityGoodsList,ListUtil.SIZE);
        if (CollUtil.isNotEmpty(updateGoodsList)) {
            activityGoodsService.updateBatchById(updateGoodsList,ListUtil.SIZE);
        }
    }

    private void saveBit(Long depotId) {
        List<ProductStockDetailsVo> tpsList = productStockServiceRpc.getAllByDepotId2(depotId);
        //拷贝新的list集合
        List<ProductStockDetailsVo> newList = tpsList.stream().collect(Collectors.toList());
        // 过滤去重
        List<Long> tmpList=Lists.newArrayList();
        tpsList = tpsList.stream().filter(
                v -> {
                    boolean flag = !tmpList.contains(v.getProductId());
                    tmpList.add(v.getProductId());
                    return flag;
                }
        ).collect(Collectors.toList());
        //整合list，库存进行叠加
        if(CollUtil.isNotEmpty(tmpList)&&CollUtil.isNotEmpty(newList)){
            for(ProductStockDetailsVo detailsVo:tpsList){
                for(ProductStockDetailsVo vo:newList){
                    if(detailsVo.getProductId().equals(vo.getProductId())){
                        detailsVo.setStock(detailsVo.getStock()+1);
                    }
                }
                if(!detailsVo.getStock().equals("1")){
                    detailsVo.setStock(detailsVo.getStock()-1);
                }
            }
        }


      List<List<ProductStockDetailsVo>>  ppList= ListUtil.fixedGrouping(tpsList,100);
        for(List<ProductStockDetailsVo> tmp:ppList){
            List<ProductStockTmp> pst = new ArrayList<ProductStockTmp>();
            for (ProductStockDetailsVo vo : tmp) {
                ProductStockTmp pt = new ProductStockTmp();
                BeanUtils.copyProperties(vo, pt);
                pt.setEnableStock(pt.getStock());
                pst.add(pt);
            }
            saveBatch(pst,ListUtil.SIZE);
        }

    }

}
